/*
 * @Author: funlee
 * @Email: i@funlee.cn
 * @Date: 2018-11-05 21:55:06
 * @Description: 百度地图 
 */

 var mapvLayer = {}

define(function () {
  var BdMap = {
    state: {
      Map: {}
    },
    renderMap: function (data, isRenderDotes) {
      var map = new BMap.Map('allmap');

      map.enableScrollWheelZoom() // 开启鼠标滚轮缩放

      var styleJson = [
        {
          "featureType": "water",
          "elementType": "all",
          "stylers": {
            "color": "#01193dff"
          }
        },
        {
          "featureType": "highway",
          "elementType": "geometry.fill",
          "stylers": {
            "color": "#000000"
          }
        },
        {
          "featureType": "highway",
          "elementType": "geometry.stroke",
          "stylers": {
            "color": "#147a92"
          }
        },
        {
          "featureType": "arterial",
          "elementType": "geometry.fill",
          "stylers": {
            "color": "#000000"
          }
        },
        {
          "featureType": "arterial",
          "elementType": "geometry.stroke",
          "stylers": {
            "color": "#0b3d51"
          }
        },
        {
          "featureType": "local",
          "elementType": "geometry",
          "stylers": {
            "color": "#000000"
          }
        },
        {
          "featureType": "land",
          "elementType": "all",
          "stylers": {
            "color": "#08304b"
          }
        },
        {
          "featureType": "railway",
          "elementType": "geometry.fill",
          "stylers": {
            "color": "#000000"
          }
        },
        {
          "featureType": "railway",
          "elementType": "geometry.stroke",
          "stylers": {
            "color": "#08304b"
          }
        },
        {
          "featureType": "subway",
          "elementType": "geometry",
          "stylers": {
            "lightness": -70
          }
        },
        {
          "featureType": "building",
          "elementType": "geometry.fill",
          "stylers": {
            "color": "#000000"
          }
        },
        {
          "featureType": "all",
          "elementType": "labels.text.fill",
          "stylers": {
            "color": "#857f7f"
          }
        },
        {
          "featureType": "all",
          "elementType": "labels.text.stroke",
          "stylers": {
            "color": "#000000"
          }
        },
        {
          "featureType": "building",
          "elementType": "geometry",
          "stylers": {
            "color": "#022338"
          }
        },
        {
          "featureType": "green",
          "elementType": "geometry",
          "stylers": {
            "color": "#062032"
          }
        },
        {
          "featureType": "boundary",
          "elementType": "all",
          "stylers": {
            "color": "#3592cfff"
          }
        },
        {
          "featureType": "manmade",
          "elementType": "geometry",
          "stylers": {
            "color": "#022338"
          }
        },
        {
          "featureType": "poi",
          "elementType": "all",
          "stylers": {
            "visibility": "off"
          }
        },
        {
          "featureType": "all",
          "elementType": "labels.icon",
          "stylers": {
            "visibility": "off"
          }
        },
        {
          "featureType": "all",
          "elementType": "labels.text.fill",
          "stylers": {
            "color": "#2da0c6",
            "visibility": "on"
          }
        }
      ]
      
      map.setMapStyle({
        styleJson
      });

      map.clearOverlays() // 清除覆盖物
      if (mapvLayer && mapvLayer.destroy) {
        mapvLayer.destroy() // 销毁mapvLayer
      }

      if (isRenderDotes) {
        this.renderDote(data, map)
      } else {
        this.renderLine(data, map)
      }
    },
    renderDote(data, map) {
      map.centerAndZoom(new BMap.Point(90.404, 39.915), 4);

      var dotesSet = [];
      var dotes = [];

      data.forEach(function (d) {
        dotesSet.push(d[0].join('~'))
        dotesSet.push(d[1].join('~'))
      });

      var dataset = [...new Set(dotesSet)];

      dataset.forEach(function (d) {
        var points = d.split('~')
        dotes.push(points)
      })

      // 添加定位点
      dotes.forEach(function (d) {
        var marker = new BMap.Marker(new BMap.Point(d[0], d[1]), {
          icon: new BMap.Icon('http://api0.map.bdimg.com/images/marker_red_sprite.png', new BMap.Size(20, 13), { // 默认大小[39, 25]
            imageSize: new BMap.Size(20, 13)
         })
        }); // 创建标注
        map.addOverlay(marker);
      })

    },
    renderLine(data, map) {
      map.centerAndZoom(new BMap.Point(116.404, 34.915), 6);

      var node_data = {};
      var edge_data = []

      data.forEach((d, i) => {
        node_data[2 * i] = {
          x: d[0][0],
          y: d[0][1]
        }
        node_data[2 * i + 1] = {
          x: d[1][0],
          y: d[1][1]
        }
        edge_data.push({
          source: 2 * i + 1,
          target: 2 * i
        })
      })

      var fbundling = mapv.utilForceEdgeBundling()
        .nodes(node_data)
        .edges(edge_data);

      var results = fbundling();

      var data = [];
      var timeData = [];

      for (var i = 0; i < results.length; i++) {
        var line = results[i];
        var coordinates = [];
        for (var j = 0; j < line.length; j++) {
          coordinates.push([line[j].x, line[j].y]);
          timeData.push({
            geometry: {
              type: 'Point',
              coordinates: [line[j].x, line[j].y]
            },
            count: 1,
            time: j
          });
        }
        data.push({
          geometry: {
            type: 'LineString',
            coordinates: coordinates
          }
        });
      }

      var dataSet = new mapv.DataSet(data);

      var options = {
        strokeStyle: 'rgba(190, 20, 32, 0.3)',
        globalCompositeOperation: 'lighter',
        shadowColor: 'rgba(121, 22, 43, 0.5)',
        shadowBlur: 10,
        lineWidth: 2.0,
        draw: 'simple'
      }

      mapvLayer = new mapv.baiduMapLayer(map, dataSet, options);

      var dataSet = new mapv.DataSet(timeData);

      var options = {
        fillStyle: 'rgba(255, 0, 0, 0.9)',
        globalCompositeOperation: 'lighter',
        size: 1.5,
        animation: {
          type: 'time',
          stepsRange: {
            start: 0,
            end: 100
          },
          trails: 1,
          duration: 5,
        },
        draw: 'simple'
      }

      mapvLayer = new mapv.baiduMapLayer(map, dataSet, options);

    }
  }
  return BdMap
})

